package com.hotagent;

import com.hotagent.mxbean.Reload;
import com.hotagent.reload.Reloader;

import java.lang.instrument.Instrumentation;

/**
 * 热更
 */
public class HotAgent {
    public static void premain(String agentArgs, Instrumentation ins) {
        regMBean(new Reloader(ins));
    }

    public static void agentmain(String agentArgs, Instrumentation ins) {
        premain(agentArgs, ins);
    }

    private static void regMBean(Reloader reloader) {
        try {
            javax.management.MBeanServer mbs = java.lang.management.ManagementFactory.getPlatformMBeanServer();
            mbs.registerMBean(new Reload(reloader), new javax.management.ObjectName("HotAgent:type=Reload"));
            LogUtil.hotAgent.info("HotAgent begin");
        } catch (Throwable e) {
            LogUtil.hotAgent.error("HotAgent error", e);
        }
    }
}